package com.example.springboot.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @Description: AOP监控性能
 * @Author wenlinyu
 * @Date: 2020/12/21 14:59
 * @Version 1.0
 */
@Component
@Aspect
public class ServiceAspect {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 切入点表达式
     */
    @Pointcut("within(com.example.springboot.service..*)")
    public void serviceAspect() {

    }

    /**
     * 环绕通知
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("serviceAspect()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        logger.info("服务层方法：{}--执行时间：{}ms", joinPoint.getSignature(), System.currentTimeMillis() - start);
        return result;
    }
}
